﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Utils
{
    public class Verification
    {
        /// <summary>
        /// 坐标格式校验    errorProvider 报错控件    textBox 文本    v 几维坐标
        /// </summary>
        public static void Coord(ErrorProvider errorProvider, TextBox textBox, int v)
        {
            //接受输入的字符串
            string a = textBox.Text;
            string[] vsa = Regex.Split(a, ",|，");
            int i = 0;
            foreach (var item in vsa)
            {
                if (IsNumber(item) != true || item.Length == 0 || vsa.Count() != v)
                {
                    i++;
                    errorProvider.SetError(textBox, "请输入正确的坐标(x,y)!");
                }
            }
            if (i == 0 || a.Length == 0)
            {
                errorProvider.Clear();
            }
        }
        /// <summary>
        /// 数据格式校验--正数    errorProvider 报错控件    textBox 文本
        /// </summary>
        public static void Data(ErrorProvider errorProvider, TextBox textBox)
        {
            string text = textBox.Text;
            bool verdict = Regex.IsMatch(text, @"(^\d+$)|(^[\d]+\.{1}[0-9]+$)");
            int i = 0;
            if (verdict == false)
            {
                i++;
                errorProvider.SetError(textBox, "请输入正数");
            }
            if (i == 0 || text.Length == 0)
            {
                errorProvider.Clear();
            }
        }
        /// <summary>
        /// 判断是否为数字,利用正则表达式
        /// </summary>
        public static bool IsNumber(string item)
        {
            bool v = Regex.IsMatch(item, @"(^-?[0-9]{1,}$)|(^-?[0-9]{1,}\.{1}[0-9]{1,}$)");
            return v;
        }
    }
}
